import os, sys, re

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + '../')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'riscv_build_dataset.settings')

import django

django.setup()

from obs.models import obs_package, obs_project, obs_package_build_log, obs_job_history
from obs.mongo_log import OBSLog
from django.db.models import Count

o = OBSLog()

def get_build_logs(project_name: str, package_name:str):
    project = obs_project.objects.get(project_name=project_name)
    package = obs_package.objects.get(project=project, package_name=package_name)
    logs = obs_package_build_log.objects.filter(project=project, package=package)
    mongo_logs = o.get_all_log(package.id)

    print(logs.count(), mongo_logs.count())


    pattern = r"srcmd5 '[0-9a-f]{32}'"
    for log in mongo_logs:
        log_text = log.read().decode('utf-8')
        find = re.search(pattern, log_text)
        if find:
            srcmd5 = find.group().partition("'")[2][:-1]
            print(srcmd5)

            job_hist = obs_job_history.objects.filter(srcmd5=srcmd5)
            print(job_hist)


def more_logs():
    duplicates = (
        obs_package_build_log.objects.values('package', 'project')  # 只关注这两个字段
        .annotate(name_count=Count('package'))  # 计算每个名字的数量
        .filter(name_count__gt=1)  # 只保留数量大于1的结果
    )
    for d in duplicates:
        print(d)

if __name__ == '__main__':
    project_name = 'openEuler:22.09'
    package_name = 'mybatis'
    get_build_logs(project_name, package_name)
    # more_logs()